home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / gnome-menus / examples / gnome-menus-ls.py next >
Encoding:
Python Source  |  2009-03-17  |  2.8 KB  |  95 lines

  1. # vim: set ts=4 sw=4 et:
  2.  
  3. #
  4. # Copyright (C) 2008 Novell, Inc.
  5. #
  6. # Authors: Vincent Untz <vuntz@gnome.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #
  22.  
  23. import optparse
  24. import sys
  25.  
  26. import gmenu
  27.  
  28. def print_entry(entry, path):
  29.     if entry.get_is_excluded():
  30.         excluded = ' <excluded>'
  31.     else:
  32.         excluded = ''
  33.  
  34.     print '%s\t%s\t%s%s' % (path, entry.get_desktop_file_id(), entry.get_desktop_file_path(), excluded)
  35.  
  36. def print_directory(dir, parent_path = None):
  37.     if not parent_path:
  38.         path = '/'
  39.     else:
  40.         path = '%s%s/' % (parent_path, dir.get_name())
  41.  
  42.     for item in dir.get_contents():
  43.         type = item.get_type()
  44.         if type == gmenu.TYPE_ENTRY:
  45.             print_entry(item, path)
  46.         elif type == gmenu.TYPE_DIRECTORY:
  47.             print_directory(item, path)
  48.         elif type == gmenu.TYPE_ALIAS:
  49.             aliased = item.get_item()
  50.             if aliased.get_type() == gmenu.TYPE_ENTRY:
  51.                 print_entry(aliased, path)
  52.         elif type in [ gmenu.TYPE_HEADER, gmenu.TYPE_SEPARATOR ]:
  53.             pass
  54.         else:
  55.             print >> sys.stderr, 'Unsupported item type: %s' % type
  56.  
  57. def main(args):
  58.     parser = optparse.OptionParser()
  59.  
  60.     parser.add_option('-f', '--file', dest='file',
  61.                       help='Menu file')
  62.     parser.add_option('-i', '--include-excluded', dest='exclude',
  63.                       action='store_true', default=False,
  64.                       help='Include <Exclude>d entries')
  65.     parser.add_option('-n', '--include-nodisplay', dest='nodisplay',
  66.                       action='store_true', default=False,
  67.                       help='Include NoDisplay=true entries')
  68.  
  69.     (options, args) = parser.parse_args()
  70.  
  71.     if options.file:
  72.         menu_file = options.file
  73.     else:
  74.         menu_file = 'applications.menu'
  75.  
  76.     flags = gmenu.FLAGS_NONE
  77.     if options.exclude:
  78.         flags |= gmenu.FLAGS_INCLUDE_EXCLUDED
  79.     if options.nodisplay:
  80.         flags |= gmenu.FLAGS_INCLUDE_NODISPLAY
  81.  
  82.     tree = gmenu.lookup_tree(menu_file, flags)
  83.     root = tree.get_root_directory()
  84.  
  85.     if not root:
  86.         print 'Menu tree is empty'
  87.     else:
  88.         print_directory(root)
  89.  
  90. if __name__ == '__main__':
  91.     try:
  92.       main(sys.argv)
  93.     except KeyboardInterrupt:
  94.       pass
  95.